package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Title: RemoveCookie
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/25 10:11
 */
@WebServlet("/remove")
public class RemoveCookie extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取cookies
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            // 移除指定的cookie
//            if( cookie.getName().equals("user"))  {
//                cookie.setMaxAge(0);
//            }
            // 所有的cookie都移除
            cookie.setMaxAge(0);
            // 将cookie发送给浏览器
            resp.addCookie(cookie);

        }

        resp.getWriter().write("success!");
    }
}
